package com.app.service;

import com.app.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * @Author: cn
 * @Description: TODO
 * @Date: 2025/7/14 15:34
 * @Version: 1.0
 */
@Service
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.selectByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        return new org.springframework.security.core.userdetails.User(
                user.getUsername(),
                user.getPassword_hash(),  // 注意这里使用的是你的密码字段名
                true,       // 账户是否启用
                true,       // 账户是否未过期
                true,       // 凭证是否未过期
                true,       // 账户是否未锁定
                Collections.emptyList()  // 权限列表
        );
    }
}
